﻿using Corina.Common;
using Corina.Datamodel.Base;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Service
{
    public class AccountVerifyService : ServiceBase
    {
        public ResultValue VerifyAccount<T>(string connectionString, string account, string password) where T : DatamodelEntityBase, new()
        {
            ResultValue result = ResultValue.Unknown;
            var accounts = DbOperator.GetEntities<T>(connectionString, new List<string>() { "Account" }, new List<object>() { account });
            if (accounts != null && accounts.Count > 0)
            {
                var item = accounts.FirstOrDefault();
                if (item.GetPropertyValue("Password").ToString() != password)
                {
                    result = ResultValue.PasswordError;
                    //result.Description = "密码错误！";
                }
                else
                {
                    AccountStatusValue accountStatus = EnumParser<AccountStatusValue>.Parse(item.GetPropertyValue("AccountStatus").ToString());
                    switch (accountStatus)
                    {
                        case AccountStatusValue.Block:
                            result = ResultValue.Block;
                            //result.Description = "账号已被锁定！";
                            break;
                        case AccountStatusValue.Pending:
                            result = ResultValue.Pending;
                            //result.Description = "账号暂未通过审核！";
                            break;
                        case AccountStatusValue.Removed:
                            result = ResultValue.Removed;
                            //result.Description = "账号已被管理员移除！";
                            break;
                        case AccountStatusValue.Normal:
                            result = ResultValue.Succeed;
                            break;
                    }
                }
            }
            else
            {
                result = ResultValue.NullAccount;
            }

            return result;
        }

        public bool ExistAccount<T>(string connectionString, string account) where T : DatamodelEntityBase, new()
        {
            return DbOperator.Exists<T>(connectionString, new List<string>() { "Account" }, new List<object>() { account });
        }

        public T GetAccountInfo<T>(string connectionString, string account) where T : DatamodelEntityBase, new()
        {
            return DbOperator.GetEntities<T>(connectionString, new List<string>() { "Account" }, new List<object>() { account }).FirstOrDefault();
        }

        public T GetAccountInfo<T>(string connectionString, int accountId) where T : DatamodelEntityBase, new()
        {
            return DbOperator.GetEntities<T>(connectionString, new List<string>() { "Id" }, new List<object>() { accountId }).FirstOrDefault();
        }

        public RoleInfo GetManagerRole(string connectionString, int id)
        {
            return DbOperator.GetEntity<RoleInfo>(connectionString, new List<string>() { "Id" }, new List<object>() { id });
        }

        public RoleInfo GetManagerRole(string connectionString, string account)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM t_role AS tr ");
            builder.Append("LEFT JOIN CORINA_ACCO.dbo.t_manager AS tm ON tm.RoleId = tr.Id ");
            builder.Append("WHERE tm.Account = @Account ");

            return DbOperator.GetEntity<RoleInfo>(connectionString, builder.ToString(), new SqlParameter("@Account", account));
        }

        public UserLevelInfo GetUserLevel(string connectionString, int id)
        {
            return DbOperator.GetEntity<UserLevelInfo>(connectionString, new List<string>() { "Id" }, new List<object>() { id });
        }

        public UserLevelInfo GetUserLevel(string connectionString, string account)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM t_user_level AS tul ");
            builder.Append("LEFT JOIN CORINA_ACCO.dbo.t_user AS tu ON tu.UserLevelId = tul.Id ");
            builder.Append("WHERE tu.Account = @Account ");

            return DbOperator.GetEntity<UserLevelInfo>(connectionString, builder.ToString(), new SqlParameter("@Account", account));
        }

        public AvatarInfo GetAccountAvatar(string connectionString, int accountId, AccountTypeValue accountType)
        {
            return DbOperator.GetEntity<AvatarInfo>(connectionString, new List<string>() { "AccountId", "AccountType" }, new List<object>() { accountId, (int)accountType });
        }

        protected bool UpdateAvatar(string connectionString, int accountId, string fileName, AccountTypeValue accountType)
        {
            AvatarInfo entity = new AvatarInfo { AccountId = accountId, Url = fileName, AccountType = accountType };
            EntityOperation operation = EntityOperation.Insert;
            AvatarInfo exist = GetAccountAvatar(connectionString, accountId, accountType);
            if (exist != null)
            {
                operation = EntityOperation.Update;
                entity.Id = exist.Id;
            }

            return DbOperator.UpdateEntity(connectionString, entity, operation);
        }

        protected bool UpdateStatus<T>(string connectionString, int accountId, AccountStatusValue status) where T : DatamodelEntityBase, new()
        {
            return DbOperator.UpdateEntityById<T>(connectionString, accountId, new List<string>() { "AccountStatus" }, new List<object>() { (int)status });
        }

        protected bool UpdateStatus<T>(string connectionString, string account, AccountStatusValue status) where T : DatamodelEntityBase, new()
        {
            return DbOperator.UpdateEntity<T>(connectionString, "Account", account, new List<string>() { "AccountStatus" }, new List<object>() { (int)status });
        }
    }
}
